﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.BasManage
{
    public partial class ProductView : BasePage
    {
        public Guid ProductID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ProductID"]))
                {
                    return new Guid(Request.QueryString["ProductID"]);
                }
                else
                    return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtRegCertificateDate.Attributes.Add("onclick", "WdatePicker();");

            ddlTrademark.DataSource = new BasProductBll().QueryProductTrademark();
            ddlTrademark.DataTextField = "Trademark";
            ddlTrademark.DataValueField = "Trademark";
            ddlTrademark.DataBind();

            ddlCateType.DataSource = new BasProductBll().QueryProductCate();
            ddlCateType.DataTextField = "CateName";
            ddlCateType.DataValueField = "CateCode";
            ddlCateType.DataBind();

            ddlCateTypeEx.DataSource = new BasProductBll().QueryProductCateEx();
            ddlCateTypeEx.DataTextField = "CateName";
            ddlCateTypeEx.DataValueField = "CateCode";
            ddlCateTypeEx.DataBind();

            if (!IsPostBack)
            {
                BasProductEntity entity = null;
                MasterDataBll bll = new MasterDataBll();
                entity = bll.Get<BasProductEntity>(ProductID);

                ltlProductCode.Text = entity.ProductCode;
                ltlProductName.Text = entity.ProductName;
                txtAlias.Text = entity.Alias;
                ltlStandard.Text = entity.Standard;
                ltlUnit.Text = entity.Unit;
                ltlOther.Text = entity.Other;
                txtRegCertificateDate.Text = entity.RegCertificateDate.ToString("yyyy-MM-dd");
                ddlTrademark.SelectedValue = entity.Trademark;
                ddlTrademark.SelectedValue = entity.Trademark;
                ddlCateType.SelectedValue = entity.CateCode;
                ddlCateTypeEx.SelectedValue = entity.CateCodeEx;
                rbCompany.SelectedValue = entity.Company;

                List<BasProductEntity> list = new BasProductBll().GetAll<BasProductEntity>(" and ProductType=2");
                cblInstrument.DataSource = list;
                cblInstrument.DataTextField = "ProductCode";
                cblInstrument.DataValueField = "ProductID";
                cblInstrument.DataBind();

                List<RelationInstrumentReagentEntity> list1 = new RelationInstrumentReagentBll().GetAll<RelationInstrumentReagentEntity>(" and ReagentID='"+entity.ProductID.ToString()+"'");

                foreach (ListItem item in cblInstrument.Items)
                {
                    Predicate<RelationInstrumentReagentEntity> predicate =
                        delegate(RelationInstrumentReagentEntity ent)
                        {
                            if (ent.InstrumentID == new Guid(item.Value)) return true;
                            return false;
                        };
                    bool IsExists = list1.Exists(predicate);
                    if (IsExists)
                        item.Selected = true;
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BasProductEntity entity = null;
            MasterDataBll bll = new MasterDataBll();
            entity = bll.Get<BasProductEntity>(ProductID);

            entity.Trademark = ddlTrademark.SelectedValue.ToString();
            entity.Alias = txtAlias.Text;
            entity.CateCode = ddlCateType.SelectedValue;
            entity.CateCodeEx = ddlCateTypeEx.SelectedValue;
            entity.Company = rbCompany.SelectedValue;
            entity.ProductName = ltlProductName.Text;

            DateTime dt = DateTime.Now;

            if (DateTime.TryParse(txtRegCertificateDate.Text, out dt))
            {
                entity.RegCertificateDate = dt;
            }


            List<RelationInstrumentReagentEntity> RelationInstrumentList = new List<RelationInstrumentReagentEntity>();
            RelationInstrumentReagentEntity RelationInstrumentEntity = null;
            foreach (ListItem item in cblInstrument.Items)
            {
                if (item.Selected)
                {
                    RelationInstrumentEntity = new RelationInstrumentReagentEntity();
                    RelationInstrumentEntity.InstrumentID = new Guid(item.Value);
                    RelationInstrumentEntity.ReagentID = entity.ProductID;
                    RelationInstrumentEntity.Deleted = false;
                    RelationInstrumentList.Add(RelationInstrumentEntity);
                }
            }


            bll.Update(entity);
            new RelationInstrumentReagentBll().saveInstrumentReagent(RelationInstrumentList,entity.ProductID);



            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功.');parent.$.ligerDialog.hide();", true);
        }

    }
}